package com.example.myapplication3.thread;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import androidx.annotation.NonNull;

public class WorkerThread extends Thread{
    private Handler workerHandler;
    public WorkerThread() {
    }
    @Override
    public void run() {
        Looper.prepare();
        workerHandler =new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what==1){
                    System.out.println("处理任务: " + msg.obj);
                }
                super.handleMessage(msg);
            }
        };
        Looper.loop();
        super.run();
    }
    public void sendTask(String task) {
        if (workerHandler != null) {
            Message msg = Message.obtain();
            msg.what = 1;
            msg.obj = task;
            workerHandler.sendMessage(msg);
        }
    }
}

